package net.jacqeush.mini.rpc.client.configuration;

import net.jacqeush.mini.rpc.client.Client;

import net.jacqeush.mini.rpc.client.processor.RpcBeanPostProcessor;
import net.jacqeush.mini.rpc.client.properties.RpcClientProperties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;


/**
 * @author Jacquesh
 * @version 1.0
 * @date 2020/10/22 2:00
 */
@Configuration(
        proxyBeanMethods = false
)

@EnableConfigurationProperties(RpcClientProperties.class)
public class MiniRpcClientAutoConfiguration implements Ordered, ApplicationContextAware {
    private  ApplicationContext applicationContext;
    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext=applicationContext;
    }

    @ConditionalOnProperty(name = "mini.rpc.client.enabled", havingValue = "true")
    @Configuration(
            proxyBeanMethods = false
    )
    protected static class ConfigClientinitializeConfiguration {
        private  RpcClientProperties config;
        public ConfigClientinitializeConfiguration(RpcClientProperties config) {
          this.config=config;
        }
        @Bean
         public Client getInitializeClient(){
          return Client.getInstance().initialize(config.getAddress(),config.getTimeout());
        }
        @Bean
        public RpcBeanPostProcessor getRpcBeanPostProcessor(Client client,RpcClientProperties config){
            return new RpcBeanPostProcessor(client,config);
        }
    }
}

//    public static void sync() {
//        //	rpcClient
//        Client rpcClient = new Client();
//        rpcClient.init("127.0.0.1:8765", 3000);
//        HelloService helloService = rpcClient.invokeSync(HelloService.class);
//        String result = helloService.hello("zhang3");
//        System.err.println(result);
//    }
//
//    public static void async() throws InterruptedException, ExecutionException {
//        Client rpcClient = new Client();
//        rpcClient.init("127.0.0.1:8765", 3000);
//        RpcAsyncProxy proxy = rpcClient.invokeAsync(HelloService.class);
//        RpcFuture future = proxy.call("hello", "li4");
//        RpcFuture future2 = proxy.call("hello", new User("001", "wang5"));
//
//        Object result = future.get();
//        Object result2 = future2.get();
//        System.err.println("result: " + result);
//        System.err.println("result2: " + result2);
//
//    }




